package com.homework1_4.itheima.service.impl;

import com.homework1_4.itheima.bean.Student;
import com.homework1_4.itheima.mapper.StudentMapper;
import com.homework1_4.itheima.service.StudentService;
import com.homework1_4.itheima.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
/*
    业务层实现类
 */
public class StudentServiceImpl implements StudentService {

    @Override
    public List<Student> selectAll() {
        //获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        //获取StudentMapper接口的实现类对象
        // StudentMapper mapper = new StudentMapperImpl();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //通过实现类对象调用方法，接收结果
        List<Student> list = mapper.selectAll();

        //释放资源
        sqlSession.close();

        //返回结果
        return list;
    }

    @Override
    public Student selectById(Integer id) {
        //获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        //获取StudentMapper接口的实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //通过实现类对象调用方法，接收结果
        Student stu = mapper.selectById(id);

        //释放资源
        sqlSession.close();

        //返回结果
        return stu;
    }

    @Override
    public Integer insert(Student stu) {
        //获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        //获取StudentMapper接口的实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //通过实现类对象调用方法，接收结果
        Integer result = mapper.insert(stu);

        //释放资源
        sqlSession.close();

        //返回结果
        return result;
    }

    @Override
    public Integer update(Student stu) {
        //获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        //获取StudentMapper接口的实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //通过实现类对象调用方法，接收结果
        Integer result = mapper.update(stu);

        //释放资源
        sqlSession.close();

        //返回结果
        return result;
    }

    @Override
    public Integer delete(Integer id) {
        //获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        //获取StudentMapper接口的实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //通过实现类对象调用方法，接收结果
        Integer result = mapper.delete(id);

        //释放资源
        sqlSession.close();

        //返回结果
        return result;
    }

    //根据姓名或年龄查询功能
    @Override
    public List<Student> selectByNameOrAge(String name, Integer age) {
        //获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        //获取StudentMapper接口的实现类对象
        // StudentMapper mapper = new StudentMapperImpl();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //通过实现类对象调用方法，接收结果
        List<Student> list = mapper.selectByNameOrAge(name,age);

        //释放资源
        sqlSession.close();

        //返回结果
        return list;
    }

    @Override
    public List<Student> selectCondition(Student stu) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectCondition(stu);
        sqlSession.close();
        return students;
    }

    @Override
    public List<Student> selectByIds(List<Integer> ids) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByIds(ids);
        sqlSession.close();
        return students;
    }
}
